package main

import "fmt"

func main() {
	fmt.Println(lengthOfLastWord("moon"))
}

func lengthOfLastWord(str string) int {
	j := len(str) - 1
	for j >= 0 && str[j] == ' ' {
		j--
	}

	if j < 0 {
		return 0
	}

	i := j

	for i >= 0 && str[i] != ' ' {
		i--
	}

	return j - i
}
